package  com.sat.common.widget.commom

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.annotation.StyleRes
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import  com.sat.common.R
import  com.sat.common.utils.ScreenUtil


/**
 * 通用DialogFragment基类
 *
 * @author DavenXie
 * Created by Daven on 2018/7/5.
 */
abstract class BaseDialogFragment : DialogFragment() {
    private var dimAmount = 0.5f //灰度深浅
    private var showBottom = false //是否底部显示 = false
    private var outCancel = true //是否点击外部取消
    private var widthRatio = 0f
    private var heightRatio = 0f

    @StyleRes
    private var animStyle = 0

    @LayoutRes
    protected var layoutId = 0
    abstract fun intLayoutId(): Int
    abstract fun convertView(
        holder: ViewHolderHelper?,
        dialog: BaseDialogFragment?
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.baseDialogFragment)
        layoutId = intLayoutId()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(layoutId, container, false)
        convertView(ViewHolderHelper.create(view), this)
        return view
    }

    override fun onStart() {
        super.onStart()
        initParams()
    }

    private fun initParams() {
        val window = dialog!!.window
        if (window != null) {
            val lp = window.attributes
            //调节灰色背景透明度[0-1]，默认0.5f
            lp.dimAmount = dimAmount
            //是否在底部显示
            if (showBottom) {
                lp.gravity = Gravity.BOTTOM
                if (animStyle == 0) {
                    animStyle = R.style.commonDialogFragmentAnim
                }
            }
            if (widthRatio > 0.0 && widthRatio < 1.0) {
                val screenWidth: Int = ScreenUtil.getScreenWidth(requireContext())
                lp.width = (screenWidth * widthRatio).toInt()
            }
            if (heightRatio > 0.0 && heightRatio < 1.0) {
                val screenHeight: Int = ScreenUtil.getScreenHeight(requireContext())
                lp.height = (screenHeight * heightRatio).toInt()
            }
            //设置dialog进入、退出的动画
            window.setWindowAnimations(animStyle)
            window.attributes = lp
        }
        isCancelable = outCancel
    }

    fun setDimAmount(dimAmount: Float): BaseDialogFragment {
        this.dimAmount = dimAmount
        return this
    }

    fun setShowBottom(showBottom: Boolean): BaseDialogFragment {
        this.showBottom = showBottom
        return this
    }

    fun setOutCancel(outCancel: Boolean): BaseDialogFragment {
        this.outCancel = outCancel
        return this
    }

    fun setAnimStyle(@StyleRes animStyle: Int): BaseDialogFragment {
        this.animStyle = animStyle
        return this
    }

    fun setWidthRatio(widthRatio: Float): BaseDialogFragment {
        this.widthRatio = widthRatio
        return this
    }

    fun setHeightRatio(heightRatio: Float): BaseDialogFragment {
        this.heightRatio = heightRatio
        return this
    }

    fun show(manager: FragmentManager): BaseDialogFragment {
        val ft = manager.beginTransaction()
        if (this.isAdded) {
            ft.remove(this).commit()
        }
        ft.add(this, this.javaClass.simpleName)
        ft.commitAllowingStateLoss()
        return this
    }

    companion object {
        private const val TAG = "BaseDialogFragment"
    }
}